package com.jty.lib_base.widget

import android.content.Intent
import android.os.Bundle
import androidx.core.content.ContextCompat
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import com.czl.lib_base.config.AppConstants
import com.jty.lib_base.R
import com.jty.lib_base.activity.CameraActivity
import com.jty.lib_base.binding.command.BindingAction
import com.jty.lib_base.binding.command.BindingCommand
import com.jty.lib_base.databinding.BaseSelectPhotoPopBinding
import com.jty.lib_base.extension.MyGlideEngine
import com.jty.lib_base.utils.DayModeUtil
import com.jty.lib_base.utils.ImageSizeFilter
import com.jty.lib_base.utils.PermissionUtil
import com.lxj.xpopup.core.BottomPopupView
import com.lxj.xpopup.util.XPopupUtils
import com.zhihu.matisse.Matisse
import com.zhihu.matisse.MimeType
import com.zhihu.matisse.filter.Filter


/**
 * author: zhouyh
 * created on: 2022/3/11 4:37 下午
 * description:拍照选项
 */
class SelectPhotoPop(private val mFragment: Fragment) : BottomPopupView(mFragment.requireContext()) {

    private var dataBinding:BaseSelectPhotoPopBinding? = null


    override fun getImplLayoutId(): Int {
        return R.layout.base_select_photo_pop
    }

    override fun onCreate() {
        super.onCreate()
        dataBinding = DataBindingUtil.bind(popupImplView)
        dataBinding?.apply {
            pop = this@SelectPhotoPop
            clRoot.background = XPopupUtils.createDrawable(
                ContextCompat.getColor(context, R.color.transparent),
                40f,
                40f,
                0f,
                0f
            )
            executePendingBindings()
        }
    }

    val onTakePhotoClickCommand: BindingCommand<Void> = BindingCommand(BindingAction {
        PermissionUtil.reqCamera(fragment = mFragment, callback = { allGranted, _, _->
            if (allGranted){
                mFragment.startActivityForResult(Intent(mFragment.requireContext(), CameraActivity::class.java),
                    AppConstants.Constants.CAMERA_REQUEST_CODE)
                dismiss()
            }
        })

    })

    val onAlbumClickCommand: BindingCommand<Void> = BindingCommand(BindingAction {
        PermissionUtil.reqStorage(fragment = mFragment, callback = { allGranted, _, _ ->
            if (allGranted) {
                Matisse.from(mFragment)
                    .choose(MimeType.ofImage(),false)
                    .showSingleMediaType(true) //只展示一种
                    .maxSelectable(1)
                    .theme(R.style.Matisse_Zhihu)
                    .countable(false)
                    .addFilter(ImageSizeFilter(100,100,10 * Filter.K * Filter.K))
                    .imageEngine(MyGlideEngine())
                    .thumbnailScale(0.8f)
                    .capture(false)
                    .originalEnable(false)
                    .forResult(AppConstants.Constants.ALBUM_REQUEST_CODE)
            }
        })
        dismiss()
    })

    val onCancelClickCommand: BindingCommand<Void> = BindingCommand(BindingAction {
        dismiss()
    })


    override fun onDestroy() {
        dataBinding?.unbind()
        super.onDestroy()

    }
}